package com.android.pocketguru;

import com.android.pocketguru.Fragments.SettingsFragment;
import com.android.pocketguru.Fragments.ShareFragment;
import com.android.pocketguru.Fragments.StartUpFragment;

import android.support.v4.app.Fragment;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends FragmentActivity {

	// Notification
    private NotificationManager nManager;
    private void startNotification() {

	NotificationCompat.Builder mBuilder =
	        new NotificationCompat.Builder(this)
	        .setSmallIcon(R.drawable.and)
	        .setContentTitle(getString(R.string.app_name))
	        .setContentText("Version " + getString(R.string.app_ver))
	        .setOngoing(true);

	Intent resultIntent = new Intent(this, MainActivity.class);
	TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
	stackBuilder.addParentStack(MainActivity.class);
	stackBuilder.addNextIntent(resultIntent);
	PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
	mBuilder.setContentIntent(resultPendingIntent);
	mBuilder.setOngoing(true);
	
	nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
	nManager.notify(R.string.app_name, mBuilder.build());
    }
    private void stopNotification() {
    	if (nManager != null)
    		nManager.cancel(R.string.app_name);
    }
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_container);
		
        // Check whether the activity is using the layout version with
        // the fragment_container FrameLayout. If so, we must add the first fragment
        if (findViewById(R.id.fragment_container) != null) {

            // However, if we're being restored from a previous state,
            // then we don't need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState != null) {
                return;
            }

            StartUpFragment firstFragment = new StartUpFragment();

            // In case this activity was started with special instructions from an Intent,
            // pass the Intent's extras to the fragment as arguments
            firstFragment.setArguments(getIntent().getExtras());

            // Add the fragment to the 'fragment_container' FrameLayout
            getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).commit();
        }
        
        // Setting recurring alarm to popup based on shared preference settings.
        AlarmHandler.setRecurringAlarm(getBaseContext(), Constants.ALARM_REQUEST_CODE);
    }
    
	@Override
	public void onStart(){
        super.onStart();
                
        startNotification();
	}
	
	@Override
	public void onPause()
	{
		super.onPause();
		//SharedPreferences.Editor state = getPreferences(0).edit();
	    //state.commit();
	}
	
	// Fragment selection
	private void InflateFragment(Fragment fragment)
	{
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack so the user can navigate back
        transaction.replace(R.id.fragment_container, fragment);
        transaction.addToBackStack(null);

        // Commit the transaction
        transaction.commit();
	}
	
	// Menu
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.menu_settings:
	        	InflateFragment(new SettingsFragment());
	            return true;
	        case R.id.menu_share:
	        	InflateFragment(new ShareFragment());
	            return true;

	        case R.id.menu_stop:
			    AlarmHandler.stopAlarm(getBaseContext(), Constants.ALARM_REQUEST_CODE);
			    
			    // Stop test alarm if any
			    AlarmHandler.stopAlarm(getBaseContext(), Constants.ALARM_TEST_REQUEST_CODE);
   
			    stopNotification();
		        Intent intent = new Intent(Intent.ACTION_MAIN);
			    intent.addCategory(Intent.CATEGORY_HOME);
			    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			    startActivity(intent);
			    return true;

	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
}